
package com.tanhua.backmanage.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.backmanage.PageDTO;
import com.tanhua.backmanage.service.MessagesService;


import com.tanhua.backmanage.vo.Movements;
import com.tanhua.backmanage.vo.PageResult;
import com.tanhua.dubbo.server.vo.MessageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("manage")
@Slf4j
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    /**
     * 消息翻页
     *
     * @param param
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity<PageResult> messagesList(PageDTO param) {
        try {
            PageResult pageResult = this.messagesService.messagesList(param);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询消息出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息置顶
     *
     * @param publishId
     * @return
     */
    @PostMapping("/messages/{id}/top")
    public ResponseEntity<String> messagesTop(@PathVariable("id") String publishId) {
        try {
            String message = this.messagesService.messagesTop(publishId);
            if (StringUtils.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("消息置顶出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**


     *
     * @param publishIds
     * @return
     */
    @PostMapping("/messages/pass")
    public ResponseEntity<String> messagesPass(@RequestBody List<String> publishIds) {
        try {
            String message = this.messagesService.messagesPass(publishIds);
            if (StringUtils.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("消息置顶出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息取消置顶
     *
     * @param publishId
     * @return
     */
    @PostMapping("/messages/{id}/untop")
    public ResponseEntity<String> messagesUnTop(@PathVariable("id") String publishId) {
        try {
            String message = this.messagesService.messagesUnTop(publishId);
            if (StringUtils.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("消息置顶出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息拒绝(批量)
     *
     * @param publishIds
     * @return
     */
    @PostMapping("/messages/reject")
    public ResponseEntity<String> messagesReject(@RequestBody List<String> publishIds) {
        try {
            String message = this.messagesService.messagesReject(publishIds);
            if (StringUtils.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("消息置顶出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息撤销(批量)
     *
     * @param publishIds
     * @return
     */
    @PostMapping("/messages/revocation")
    public ResponseEntity<String> messagesRevocation(@RequestBody List<String> publishIds) {
        try {
            String message = this.messagesService.messagesRevocation(publishIds);
            if (StringUtils.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("消息撤销出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息详情
     *
     * @param publishId
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity<Movements> messagesDetails(@PathVariable("id") String publishId) {
        try {
            Movements movements = this.messagesService.messagesDetails(publishId);
            if (movements != null) {
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            log.error("查询消息详情出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}

